const wxHttpHandler = require('../../utils/wxHttpHandler.js');
const api = require('../../APIConfig/apiConfig.js');

Page({
  data: {

    worlUnits: [], //企业列表
    deviceTypes: [], //设备类型
    registerNos: [], //车牌号
    isAllcheck: false, //是否全选
    deviceList: [], //设备数据
    selectWorkUnit: '选择企业', //选中的企业
    selectWorkUnitId: 0, //选中的企业id
    selectDeviceType: '设备类型', //选中的设备类型
    selectDeviceTypeId: 0, //选中的设备类型id
    selectRegisterNo: '车牌号', //选中的车牌号
    selectRegisterNoId: 0, //选中的车牌号id
  },


  onLoad: function (options) {
    this.loadVehicleUnit()
    this.loadTerminalkind()
    this.loadboundVehicle()

  },

  onShow: function () {
    this.loadTerminal()
  },


  // 新增设备
  toAddDevice: function (e) {
    wx.navigateTo({
      url: '../addDevice/addDevice',
    })
  },

  // 报警设置
  toAlarmSet: function (e) {
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '../alarmSetting/alarmSetting?terminalId=' + e.currentTarget.dataset.id,
    })
  },



  // 企业选择器
  bindWorlUnitChange: function (e) {
    this.setData({
      selectWorkUnitId: this.data.worlUnits[e.detail.value].id,
      selectWorkUnit: this.data.worlUnits[e.detail.value].name
    })
  },

  // 设备类型选择器
  bindDeviceTypeChange: function (e) {
    this.setData({
      selectDeviceTypeId: this.data.deviceTypes[e.detail.value].id,
      selectDeviceType: this.data.deviceTypes[e.detail.value].name
    })
  },


  //车牌号选择器
  bindRegisterNoChange: function (e) {
    this.setData({
      selectRegisterNoId: this.data.registerNos[e.detail.value].id,
      selectRegisterNo: this.data.registerNos[e.detail.value].name
    })
  },

  // 删除
  deleteTap: function () {
    let deleteIds = ''
    let that = this
    this.data.deviceList.forEach(function (item, index) {
      if (item.isCheck) {
        deleteIds += item.id + ","
      }
    })
    console.log(deleteIds)
    if (deleteIds == '') {
      wx.showToast({
        title: '没有选中的设备',
      })
      return
    }

    wx.showModal({
      title: '删除设备',
      content: '您确定要删除设备吗？',
      confirmText: '删除',
      confirmColor: '#ff0000',
      success(res) {
        if (res.confirm) {
          that.deleteTerminalById(deleteIds)
        }
      }
    })


  },


  // 查询
  toQuery: function () {
    this.setData({
      isAllcheck: false
    })
    this.loadTerminal()
  },

  // 全选
  allCheckTap: function () {
    let that = this
    if (this.data.isAllcheck) {
      this.setData({
        isAllcheck: false
      })
      this.data.deviceList.forEach(function (item, index) {
        let isCheckParam = "deviceList[" + index + "].isCheck"
        that.setData({
          [isCheckParam]: false,
        })
      })
    } else {
      this.setData({
        isAllcheck: true
      })

      this.data.deviceList.forEach(function (item, index) {
        let isCheckParam = "deviceList[" + index + "].isCheck"
        that.setData({
          [isCheckParam]: true
        })
      })



    }
  },

  // 单选
  checkTap: function (e) {
    let isCheckParam = "deviceList[" + e.currentTarget.dataset.index + "].isCheck"
    if (this.data.deviceList[e.currentTarget.dataset.index].isCheck) {
      this.setData({
        [isCheckParam]: false,
        isAllcheck: false
      })
    } else {
      this.setData({
        [isCheckParam]: true
      })
      // 判断是否已全选
      let isAll = true
      this.data.deviceList.forEach(function (item, index) {
        if (!item.isCheck) {
          isAll = false
        }
      })
      this.setData({
        isAllcheck: isAll
      })
    }
  },




  //企业列表
  loadVehicleUnit: function () {
    let data = {
      uid: wx.getStorageSync('id'),
    }
    const that = this
    wxHttpHandler({
      type: 'get',
      path: api.loadVehicleUnit,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          if (res.data != null && res.data.length > 0) {
            that.setData({
              worlUnits: res.data,
            })
          }
        } else {
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },


  //设备类型
  loadTerminalkind: function () {
    let data = {}
    const that = this
    wxHttpHandler({
      type: 'post',
      path: api.loadTerminalkind,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          if (res.data != null && res.data.length > 0) {
            that.setData({
              deviceTypes: res.data,
            })
          } else {
            wx.showToast({
              title: res.desc,
              icon: 'none',
              duration: 1500,
              mask: true
            });
          }
        }
      }
    })
  },


  //车牌号
  loadboundVehicle: function () {
    let data = {
      uid: wx.getStorageSync('id'),
    }
    const that = this
    wxHttpHandler({
      type: 'get',
      path: api.loadboundVehicle,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          if (res.data != null && res.data.length > 0) {
            that.setData({
              registerNos: res.data,
            })
          }
        } else {
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },


  //设备查询
  loadTerminal: function () {
    let data = {
      uid: wx.getStorageSync('id'),
      vehicleId: this.data.selectRegisterNoId,
      workUnitId: this.data.selectWorkUnitId,
      terminalkindId: this.data.selectDeviceTypeId,
    }
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.loadTerminal,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          if (res.data != null && res.data.length > 0) {
            that.setData({
              deviceList: res.data,
            })
            that.data.deviceList.forEach(function (item, index) {
              let isCheckParam = "deviceList[" + index + "].isCheck"
              that.setData({
                [isCheckParam]: false
              })
            })
          }
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },


  //删除设备
  deleteTerminalById: function (e) {
    let data = {
      ids: e
    }
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.deleteTerminalById,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          wx.hideLoading()
          wx.showToast({
            title: '已删除',
          })
          that.loadTerminal()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },






})